//
//  ByCellRedPriceOrCount.m
//  mijifen_ios
//
//  Created by mc on 2017/10/24.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByCellRedPriceOrCount.h"

@interface ByCellRedPriceOrCount()<UITextFieldDelegate>
@end

@implementation ByCellRedPriceOrCount
{
    UIView      *_bgView;
    UILabel     *_titleLabel;
    UITextField *_textField;
    UILabel     *_subTitleLabel;
    NSString    *_title;
    NSString    *_subTitle;
}

+ (instancetype)cellWithTableView:(UITableView*)tableView andTitle:(NSString *)title andSubTitle:(NSString *)subTitle{
    
    static NSString *cellIdentifier = @"ByCellRedPriceOrCount";
    ByCellRedPriceOrCount *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier andTitle:title andSubTitle:subTitle];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier andTitle:(NSString *)title andSubTitle:(NSString *)subTitle{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _title = title;
        _subTitle = subTitle;
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyBoard) name:@"hideAimKeyBoard" object:nil];
        [self createViews];
    }
    return self;
}

- (void)createViews{
    _bgView = [[UIView alloc] init];
    [self.contentView addSubview:_bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.top.equalTo(self.mas_top);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 30, 50));
    }];
    _bgView.backgroundColor = [UIColor whiteColor];
    
    _titleLabel = [[UILabel alloc] init];
    [_bgView addSubview:_titleLabel];
    CGFloat width = [_title getSizeOfString:16].width + 5;
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_bgView.mas_top).offset(10);
        make.left.equalTo(_bgView.mas_left).offset(10);
        make.size.mas_equalTo(CGSizeMake(width, 30));
    }];
    _titleLabel.font = FONT(16);
    _titleLabel.text = _title;
    
    width = [_subTitle getSizeOfString:16].width;
    _subTitleLabel = [[UILabel alloc] init];
    [_bgView addSubview:_subTitleLabel];
    [_subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_bgView.mas_top).offset(10);
        make.right.equalTo(_bgView.mas_right).offset(- 10);
        make.size.mas_equalTo(CGSizeMake(width, 30));
    }];
    _subTitleLabel.font = FONT(16);
    _subTitleLabel.textColor = [UIColor lightGrayColor];
    _subTitleLabel.text = _subTitle;
    
    _textField = [[UITextField alloc] init];
    [_bgView addSubview:_textField];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_titleLabel.mas_right).offset(10);
        make.top.equalTo(_titleLabel.mas_top);
        make.right.equalTo(_subTitleLabel.mas_left).offset(-5);
        make.bottom.equalTo(_bgView.mas_bottom).offset(-10);
    }];
    _textField.textAlignment = NSTextAlignmentRight;
    _textField.font = FONT(16);
    _textField.delegate = self;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (self.priceOrCountBlock) {
        self.priceOrCountBlock(textField.text);
    }
}

#pragma mark - 隐藏键盘
- (void)hideKeyBoard{
    [_textField resignFirstResponder];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"hideAimKeyBoard" object:nil];
}

@end
